<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class GoodsController extends Controller
{
    public function index($id)
    {
        $data = DB::table('goods')
            ->where('pid', $id)
            ->get();
        return view('admin.goods.goodsIndex', ['title' => '商品列表', 'data' => $data, 'pid' => $id]);
    }

//    单击修改是否上架
    public function saleEdit(Request $request)
    {
        $id = $request->input('id');
        $is_onsale = DB::table('goods')->where('id', $id)->first()->is_onsale;
        if ($is_onsale == 1) {
            $res = DB::table('goods')->where('id', $id)->update(['is_onsale' => 0]);
            if ($res) {
                return 0;
            } else {
                return 2;
            }
        } else {
            $res = DB::table('goods')->where('id', $id)->update(['is_onsale' => 1]);
            if ($res) {
                return 1;
            } else {
                return 2;
            }
        }
    }

//    单击修改是否热卖
    public function hotEdit(Request $request)
    {
        $id = $request->input('id');
        $hot_sale = DB::table('goods')->where('id', $id)->first()->hot_sale;
        if ($hot_sale == 1) {
            $res = DB::table('goods')->where('id', $id)->update(['hot_sale' => 0]);
            if ($res) {
                return 0;
            } else {
                return 2;
            }
        } else {
            $res = DB::table('goods')->where('id', $id)->update(['hot_sale' => 1]);
            if ($res) {
                return 1;
            } else {
                return 2;
            }
        }
    }

    public function plateDoDelete($id)
    {
        $pid = DB::table('goods as g1')->where('id', $id)->first()->pid;
        $res = DB::table('goods')->where('id', $id)->delete();
        if ($res) {
            return redirect(url("admin/goods/goodsIndex/$pid"))->with(['title' => '分类列表', 'info' => '删除成功']);
        } else {
            return back()->with(['title' => '商品预览', 'info' => '删除失败']);
        }
    }

    //添加商品视图
    public function add()
    {
        return view('admin.goods.index',['title' => '添加商品']);
    }


    //商品详情
    public function goodsDetail($id)
    {

        $data = DB::table('goods')->where('id', $id)->first();

        return view('admin.goods.goodsDetail')->with(['title' => '商品详情', 'data' => $data]);
    }

    //添加商品视图界面
    public function goodsAdd($id)
    {
        return view('admin.goods.goodsAdd')->with(['title' => '添加商品', 'id' => $id]);
    }

    //添加商品处理
    public function insert(Request $request)
    {
        $data = $request->except('_token');
        $data['addtime'] = time();
        $pid = $data['pid'];
        $res = DB::table('plate')->where('id', $pid)->first();
        if (!$res) {
            return back()->with(['info' => '添加失败']);
        }
        $data['path'] = $res->path . ',' . $pid;
        //判断文件是否上传
        if ($request->hasFile('goodspic')) {
            //判断文件上传是否有效
            if ($request->file('goodspic')->isValid()) {
                //获取文件扩展名
                $suffix = $request->file('goodspic')->getClientOriginalExtension();
                //拼接文件名
                $fileName = time() . mt_rand(10000, 99999) . '.' . $suffix;
                //移动文件
                $move = $request->file('goodspic')->move('./uploads/goodspic', $fileName);
                if ($move) {
                    $data['goodspic'] = $fileName;
                } else {
                    $data['goodspic'] = 'default.jpg';
                }
            } else {
                $data['pic'] = 'default.jpg';
            }
            $res = DB::table('goods')->insert($data);
            if ($res) {
                return redirect("/admin/goods/goodsIndex/$pid");
            } else {
                return back();
            }
        }
    }

    //商品编辑
    public function goodsEdit($id)
    {
        $data = DB::table('goods')->where('id', $id)->first();
        if (!$data) {
            return back()->with(['info' => '编辑失败']);
        }
        return view('admin.goods.goodsEdit')->with(['data' => $data, 'id' => $id]);
    }

    //商品编辑处理
    public function goodsUpdate(Request $request)
    {
        $data = $request->except('_token');
        $id = $data['id'];
        //判断文件是否上传
        if ($request->hasFile('goodspic')) {
            //判断文件上传是否有效
            if ($request->file('goodspic')->isValid()) {
                //获取文件扩展名
                $suffix = $request->file('goodspic')->getClientOriginalExtension();
                //拼接文件名
                $fileName = time() . mt_rand(10000, 99999) . '.' . $suffix;
                //移动文件
                $move = $request->file('goodspic')->move('./uploads/goodspic', $fileName);
                if ($move) {
                    $data['goodspic'] = $fileName;
                }
            }
            $res = DB::table('goods')->where('id', $id)->update($data);
            $pid = DB::table('goods')->where('id', $id)->first()->pid;
            if ($res) {
                return redirect("/admin/goods/goodsIndex/$pid")->with(['info' => '修改成功']);
            } else {
                return back();
            }
        }
        $res = DB::table('goods')->where('id', $id)->update($data);
        $pid = DB::table('goods')->where('id', $id)->first()->pid;
        if ($res) {
            return redirect("/admin/goods/goodsIndex/$pid")->with(['info' => '修改成功']);
        } else {
            return back()->with(['info' => '修改失败']);
        }

    }

    public function goodsDelete($id)
    {
        $res = DB::table('goods')->where('id', $id)->delete();
        return redirect('/admin/plate/searchGoods')->with(['info'=>'删除成功','title'=>'商品列表']);
    }

}

